home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / READPIX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-04  |  51.1 KB  |  1,432 lines

  1. /* $Id: readpix.c,v 3.8 1998/08/04 02:47:49 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: readpix.c,v $
  26.  * Revision 3.8  1998/08/04 02:47:49  brianp
  27.  * clipping in read_fast_rgba_pixels() off by one (Randy Frank)
  28.  *
  29.  * Revision 3.7  1998/07/18 03:34:24  brianp
  30.  * GL_ALPHA format was missing
  31.  *
  32.  * Revision 3.6  1998/07/17 03:24:16  brianp
  33.  * added Pixel.ScaleOrBiasRGBA field
  34.  *
  35.  * Revision 3.5  1998/03/27 04:17:52  brianp
  36.  * fixed G++ warnings
  37.  *
  38.  * Revision 3.4  1998/03/15 18:50:25  brianp
  39.  * added GL_EXT_abgr extension
  40.  *
  41.  * Revision 3.3  1998/02/08 20:22:14  brianp
  42.  * LOTS of clean-up and rewriting
  43.  *
  44.  * Revision 3.2  1998/02/01 22:29:09  brianp
  45.  * added support for packed pixel formats
  46.  *
  47.  * Revision 3.1  1998/02/01 20:47:42  brianp
  48.  * added GL_BGR and GL_BGRA pixel formats
  49.  *
  50.  * Revision 3.0  1998/01/31 21:02:29  brianp
  51.  * initial rev
  52.  *
  53.  */
  54.  
  55.  
  56. #ifdef PC_HEADER
  57. #include "all.h"
  58. #else
  59. #include <math.h>
  60. #include <stdlib.h>
  61. #include <string.h>
  62. #include "context.h"
  63. #include "depth.h"
  64. #include "feedback.h"
  65. #include "macros.h"
  66. #include "image.h"
  67. #include "pixel.h"
  68. #include "readpix.h"
  69. #include "span.h"
  70. #include "stencil.h"
  71. #include "types.h"
  72. #endif
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * Read a block of color index pixels.
  79.  */
  80. static void read_index_pixels( GLcontext *ctx,
  81.                                GLint x, GLint y,
  82.                    GLsizei width, GLsizei height,
  83.                    GLenum type, GLvoid *pixels )
  84. {
  85.    GLint i, j;
  86.  
  87.    /* error checking */
  88.    if (ctx->Visual->RGBAflag) {
  89.       gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
  90.       return;
  91.    }
  92.  
  93.    /* process image row by row */
  94.    for (j=0;j<height;j++,y++) {
  95.       GLuint index[MAX_WIDTH];
  96.       GLvoid *dest;
  97.  
  98.       (*ctx->Driver.ReadCI32Span)( ctx, width, x, y, index );
  99.  
  100.       if (ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0) {
  101.          gl_shift_and_offset_ci( ctx, width, index);
  102.       }
  103.  
  104.       if (ctx->Pixel.MapColorFlag) {
  105.          gl_map_ci(ctx, width, index);
  106.       }
  107.  
  108.       dest = gl_pixel_addr_in_image(&ctx->Pack, pixels,
  109.                          width, height, GL_COLOR_INDEX, type, 0, j, 0);
  110.  
  111.       switch (type) {
  112.      case GL_UNSIGNED_BYTE:
  113.         {
  114.                GLubyte *dst = (GLubyte *) dest;
  115.            for (i=0;i<width;i++) {
  116.           *dst++ = (GLubyte) index[i];
  117.            }
  118.         }
  119.         break;
  120.      case GL_BYTE:
  121.         {
  122.                GLbyte *dst = (GLbyte *) dest;
  123.            for (i=0;i<width;i++) {
  124.           dst[i] = (GLbyte) index[i];
  125.            }
  126.         }
  127.         break;
  128.      case GL_UNSIGNED_SHORT:
  129.         {
  130.                GLushort *dst = (GLushort *) dest;
  131.            for (i=0;i<width;i++) {
  132.           dst[i] = (GLushort) index[i];
  133.            }
  134.            if (ctx->Pack.SwapBytes) {
  135.           gl_swap2( (GLushort *) dst, width );
  136.            }
  137.         }
  138.         break;
  139.      case GL_SHORT:
  140.         {
  141.                GLshort *dst = (GLshort *) dest;
  142.            for (i=0;i<width;i++) {
  143.           dst[i] = (GLshort) index[i];
  144.            }
  145.            if (ctx->Pack.SwapBytes) {
  146.           gl_swap2( (GLushort *) dst, width );
  147.            }
  148.         }
  149.         break;
  150.      case GL_UNSIGNED_INT:
  151.         {
  152.                GLuint *dst = (GLuint *) dest;
  153.            for (i=0;i<width;i++) {
  154.           dst[i] = (GLuint) index[i];
  155.            }
  156.            if (ctx->Pack.SwapBytes) {
  157.           gl_swap4( (GLuint *) dst, width );
  158.            }
  159.         }
  160.         break;
  161.      case GL_INT:
  162.         {
  163.                GLint *dst = (GLint *) dest;
  164.            for (i=0;i<width;i++) {
  165.           dst[i] = (GLint) index[i];
  166.            }
  167.            if (ctx->Pack.SwapBytes) {
  168.           gl_swap4( (GLuint *) dst, width );
  169.            }
  170.         }
  171.         break;
  172.      case GL_FLOAT:
  173.         {
  174.                GLfloat *dst = (GLfloat *) dest;
  175.            for (i=0;i<width;i++) {
  176.           dst[i] = (GLfloat) index[i];
  177.            }
  178.            if (ctx->Pack.SwapBytes) {
  179.           gl_swap4( (GLuint *) dst, width );
  180.            }
  181.         }
  182.         break;
  183.          default:
  184.             gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
  185.       }
  186.    }
  187. }
  188.  
  189.  
  190.  
  191. static void read_depth_pixels( GLcontext *ctx,
  192.                                GLint x, GLint y,
  193.                    GLsizei width, GLsizei height,
  194.                    GLenum type, GLvoid *pixels )
  195. {
  196.    GLint i, j;
  197.    GLboolean bias_or_scale;
  198.  
  199.    /* Error checking */
  200.    if (ctx->Visual->DepthBits <= 0) {
  201.       /* No depth buffer */
  202.       gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
  203.       return;
  204.    }
  205.  
  206.    bias_or_scale = ctx->Pixel.DepthBias!=0.0 || ctx->Pixel.DepthScale!=1.0;
  207.  
  208.    if (type==GL_UNSIGNED_SHORT && sizeof(GLdepth)==sizeof(GLushort)
  209.        && !bias_or_scale && !ctx->Pack.SwapBytes) {
  210.       /* Special case: directly read 16-bit unsigned depth values. */
  211.       for (j=0;j<height;j++,y++) {
  212.          GLushort *dst = (GLushort*) gl_pixel_addr_in_image(&ctx->Pack, pixels,
  213.                          width, height, GL_DEPTH_COMPONENT, type, 0, j, 0);
  214.          (*ctx->Driver.ReadDepthSpanInt)( ctx, width, x, y, (GLdepth*) dst);
  215.       }
  216.    }
  217.    else if (type==GL_UNSIGNED_INT && sizeof(GLdepth)==sizeof(GLuint)
  218.             && !bias_or_scale && !ctx->Pack.SwapBytes) {
  219.       /* Special case: directly read 32-bit unsigned depth values. */
  220.       /* Compute shift value to scale depth values up to 32-bit uints. */
  221.       GLuint shift = 0;
  222.       GLuint max = MAX_DEPTH;
  223.       while ((max&0x80000000)==0) {
  224.          max = max << 1;
  225.          shift++;
  226.       }
  227.       for (j=0;j<height;j++,y++) {
  228.          GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack, pixels,
  229.                          width, height, GL_DEPTH_COMPONENT, type, 0, j, 0);
  230.          (*ctx->Driver.ReadDepthSpanInt)( ctx, width, x, y, (GLdepth*) dst);
  231.          for (i=0;i<width;i++) {
  232.             dst[i] = dst[i] << shift;
  233.          }
  234.       }
  235.    }
  236.    else {
  237.       /* General case (slow) */
  238.       for (j=0;j<height;j++,y++) {
  239.          GLfloat depth[MAX_WIDTH];
  240.          GLvoid *dest;
  241.  
  242.          (*ctx->Driver.ReadDepthSpanFloat)( ctx, width, x, y, depth );
  243.  
  244.          if (bias_or_scale) {
  245.             for (i=0;i<width;i++) {
  246.                GLfloat d;
  247.                d = depth[i] * ctx->Pixel.DepthScale + ctx->Pixel.DepthBias;
  248.                depth[i] = CLAMP( d, 0.0F, 1.0F );
  249.             }
  250.          }
  251.  
  252.          dest = gl_pixel_addr_in_image(&ctx->Pack, pixels,
  253.                          width, height, GL_DEPTH_COMPONENT, type, 0, j, 0);
  254.  
  255.          switch (type) {
  256.             case GL_UNSIGNED_BYTE:
  257.                {
  258.                   GLubyte *dst = (GLubyte *) dest;
  259.                   for (i=0;i<width;i++) {
  260.                      dst[i] = FLOAT_TO_UBYTE( depth[i] );
  261.                   }
  262.                }
  263.                break;
  264.             case GL_BYTE:
  265.                {
  266.                   GLbyte *dst = (GLbyte *) dest;
  267.                   for (i=0;i<width;i++) {
  268.                      dst[i] = FLOAT_TO_BYTE( depth[i] );
  269.                   }
  270.                }
  271.                break;
  272.             case GL_UNSIGNED_SHORT:
  273.                {
  274.                   GLushort *dst = (GLushort *) dest;
  275.                   for (i=0;i<width;i++) {
  276.                      dst[i] = FLOAT_TO_USHORT( depth[i] );
  277.                   }
  278.                   if (ctx->Pack.SwapBytes) {
  279.                      gl_swap2( (GLushort *) dst, width );
  280.                   }
  281.                }
  282.                break;
  283.             case GL_SHORT:
  284.                {
  285.                   GLshort *dst = (GLshort *) dest;
  286.                   for (i=0;i<width;i++) {
  287.                      dst[i] = FLOAT_TO_SHORT( depth[i] );
  288.                   }
  289.                   if (ctx->Pack.SwapBytes) {
  290.                      gl_swap2( (GLushort *) dst, width );
  291.                   }
  292.                }
  293.                break;
  294.             case GL_UNSIGNED_INT:
  295.                {
  296.                   GLuint *dst = (GLuint *) dest;
  297.                   for (i=0;i<width;i++) {
  298.                      dst[i] = FLOAT_TO_UINT( depth[i] );
  299.                   }
  300.                   if (ctx->Pack.SwapBytes) {
  301.                      gl_swap4( (GLuint *) dst, width );
  302.                   }
  303.                }
  304.                break;
  305.             case GL_INT:
  306.                {
  307.                   GLint *dst = (GLint *) dest;
  308.                   for (i=0;i<width;i++) {
  309.                      dst[i] = FLOAT_TO_INT( depth[i] );
  310.                   }
  311.                   if (ctx->Pack.SwapBytes) {
  312.                      gl_swap4( (GLuint *) dst, width );
  313.                   }
  314.                }
  315.                break;
  316.             case GL_FLOAT:
  317.                {
  318.                   GLfloat *dst = (GLfloat *) dest;
  319.                   for (i=0;i<width;i++) {
  320.                      dst[i] = depth[i];
  321.                   }
  322.                   if (ctx->Pack.SwapBytes) {
  323.                      gl_swap4( (GLuint *) dst, width );
  324.                   }
  325.                }
  326.                break;
  327.             default:
  328.                gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
  329.          }
  330.       }
  331.    }
  332. }
  333.  
  334.  
  335.  
  336.  
  337. static void read_stencil_pixels( GLcontext *ctx,
  338.                                  GLint x, GLint y,
  339.                  GLsizei width, GLsizei height,
  340.                  GLenum type, GLvoid *pixels )
  341. {
  342.    GLboolean shift_or_offset;
  343.    GLint i, j;
  344.  
  345.    if (ctx->Visual->StencilBits<=0) {
  346.       /* No stencil buffer */
  347.       gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
  348.       return;
  349.    }
  350.  
  351.    shift_or_offset = ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0;
  352.  
  353.    /* process image row by row */
  354.    for (j=0;j<height;j++,y++) {
  355.       GLvoid *dest;
  356.       GLubyte stencil[MAX_WIDTH];
  357.  
  358.       gl_read_stencil_span( ctx, width, x, y, stencil );
  359.  
  360.       if (shift_or_offset) {
  361.          gl_shift_and_offset_stencil( ctx, width, stencil );
  362.       }
  363.  
  364.       if (ctx->Pixel.MapStencilFlag) {
  365.          gl_map_stencil( ctx, width, stencil );
  366.       }
  367.  
  368.       dest = gl_pixel_addr_in_image(&ctx->Pack, pixels,
  369.                           width, height, GL_STENCIL_INDEX, type, 0, j, 0);
  370.  
  371.       switch (type) {
  372.      case GL_UNSIGNED_BYTE:
  373.             MEMCPY( dest, stencil, width );
  374.         break;
  375.      case GL_BYTE:
  376.             MEMCPY( dest, stencil, width );
  377.         break;
  378.      case GL_UNSIGNED_SHORT:
  379.         {
  380.                GLushort *dst = (GLushort *) dest;
  381.            for (i=0;i<width;i++) {
  382.           dst[i] = (GLushort) stencil[i];
  383.            }
  384.            if (ctx->Pack.SwapBytes) {
  385.           gl_swap2( (GLushort *) dst, width );
  386.            }
  387.         }
  388.         break;
  389.      case GL_SHORT:
  390.         {
  391.                GLshort *dst = (GLshort *) dest;
  392.            for (i=0;i<width;i++) {
  393.           dst[i] = (GLshort) stencil[i];
  394.            }
  395.            if (ctx->Pack.SwapBytes) {
  396.           gl_swap2( (GLushort *) dst, width );
  397.            }
  398.         }
  399.         break;
  400.      case GL_UNSIGNED_INT:
  401.         {
  402.                GLuint *dst = (GLuint *) dest;
  403.            for (i=0;i<width;i++) {
  404.           dst[i] = (GLuint) stencil[i];
  405.            }
  406.            if (ctx->Pack.SwapBytes) {
  407.           gl_swap4( (GLuint *) dst, width );
  408.            }
  409.         }
  410.         break;
  411.      case GL_INT:
  412.         {
  413.                GLint *dst = (GLint *) dest;
  414.            for (i=0;i<width;i++) {
  415.           *dst++ = (GLint) stencil[i];
  416.            }
  417.            if (ctx->Pack.SwapBytes) {
  418.           gl_swap4( (GLuint *) dst, width );
  419.            }
  420.         }
  421.         break;
  422.      case GL_FLOAT:
  423.         {
  424.                GLfloat *dst = (GLfloat *) dest;
  425.            for (i=0;i<width;i++) {
  426.           dst[i] = (GLfloat) stencil[i];
  427.            }
  428.            if (ctx->Pack.SwapBytes) {
  429.           gl_swap4( (GLuint *) dst, width );
  430.            }
  431.         }
  432.         break;
  433.          default:
  434.             gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
  435.       }
  436.    }
  437. }
  438.  
  439.  
  440.  
  441. /*
  442.  * Optimized glReadPixels for particular pixel formats:
  443.  *   GL_UNSIGNED_BYTE, GL_RGBA
  444.  * when pixel scaling, biasing and mapping are disabled.
  445.  */
  446. static GLboolean read_fast_rgba_pixels( GLcontext *ctx,
  447.                                         GLint x, GLint y,
  448.                                         GLsizei width, GLsizei height,
  449.                                         GLenum format, GLenum type,
  450.                                         GLvoid *pixels )
  451. {
  452.    /* can't do scale, bias or mapping */
  453.    if (ctx->Pixel.ScaleOrBiasRGBA || ctx->Pixel.MapColorFlag)
  454.        return GL_FALSE;
  455.  
  456.    /* can't do fancy pixel packing */
  457.    if (ctx->Pack.Alignment != 1 || ctx->Pack.SwapBytes || ctx->Pack.LsbFirst)
  458.       return GL_FALSE;
  459.  
  460.    {
  461.       GLint srcX = x;
  462.       GLint srcY = y;
  463.       GLint readWidth = width;           /* actual width read */
  464.       GLint readHeight = height;         /* actual height read */
  465.       GLint skipPixels = ctx->Pack.SkipPixels;
  466.       GLint skipRows = ctx->Pack.SkipRows;
  467.       GLint rowLength;
  468.  
  469.       if (ctx->Pack.RowLength > 0)
  470.          rowLength = ctx->Pack.RowLength;
  471.       else
  472.          rowLength = width;
  473.  
  474.       /* horizontal clipping */
  475.       if (srcX < ctx->Buffer->Xmin) {
  476.          skipPixels += (ctx->Buffer->Xmin - srcX);
  477.          readWidth  -= (ctx->Buffer->Xmin - srcX);
  478.          srcX = ctx->Buffer->Xmin;
  479.       }
  480.       if (srcX + readWidth > ctx->Buffer->Xmax)
  481.          readWidth -= (srcX + readWidth - ctx->Buffer->Xmax - 1);
  482.       if (readWidth <= 0)
  483.          return GL_TRUE;
  484.  
  485.       /* vertical clipping */
  486.       if (srcY < ctx->Buffer->Ymin) {
  487.          skipRows   += (ctx->Buffer->Ymin - srcY);
  488.          readHeight -= (ctx->Buffer->Ymin - srcY);
  489.          srcY = ctx->Buffer->Ymin;
  490.       }
  491.       if (srcY + readHeight > ctx->Buffer->Ymax)
  492.          readHeight -= (srcY + readHeight - ctx->Buffer->Ymax - 1);
  493.       if (readHeight <= 0)
  494.          return GL_TRUE;
  495.  
  496.       /*
  497.        * Ready to read!
  498.        * The window region at (destX, destY) of size (drawWidth, drawHeight)
  499.        * will be read back.
  500.        * We'll write pixel data to buffer pointed to by "pixels" but we'll
  501.        * skip "skipRows" rows and skip "skipPixels" pixels/row.
  502.        */
  503.       if (format==GL_RGBA && type==GL_UNSIGNED_BYTE) {
  504.          GLubyte *dest = (GLubyte *) pixels
  505.                          + (skipRows * rowLength + skipPixels) * 4;
  506.          GLint row;
  507.          for (row=0; row<readHeight; row++) {
  508.             (*ctx->Driver.ReadRGBASpan)(ctx, readWidth, srcX, srcY,
  509.                                         (void *) dest);
  510.             dest += rowLength * 4;
  511.             srcY++;
  512.          }
  513.          return GL_TRUE;
  514.       }
  515.       else {
  516.          /* can't do this format/type combination */
  517.          return GL_FALSE;
  518.       }
  519.    }
  520. }
  521.  
  522.  
  523.  
  524. /*
  525.  * Read R, G, B, A, RGB, L, or LA pixels.
  526.  */
  527. static void read_rgba_pixels( GLcontext *ctx,
  528.                               GLint x, GLint y,
  529.                               GLsizei width, GLsizei height,
  530.                               GLenum format, GLenum type, GLvoid *pixels )
  531. {
  532.    GLint i, j, n, s;
  533.    DEFARRAY(GLfloat, red, MAX_WIDTH);
  534.    DEFARRAY(GLfloat, green, MAX_WIDTH);
  535.    DEFARRAY(GLfloat, blue, MAX_WIDTH);
  536.    DEFARRAY(GLfloat, alpha, MAX_WIDTH);
  537.    DEFARRAY(GLfloat, luminance, MAX_WIDTH);
  538.  
  539.    /* number of components */
  540.    n = gl_components_in_format(format);
  541.    if (n <= 0) {
  542.       gl_error(ctx, GL_INVALID_ENUM, "glReadPixels(format)");
  543.       UNDEFARRAY( red );
  544.       UNDEFARRAY( green );
  545.       UNDEFARRAY( blue );
  546.       UNDEFARRAY( alpha );
  547.       UNDEFARRAY( luminance );
  548.       return;
  549.    }
  550.  
  551.    /* Size of each component */
  552.    s = gl_sizeof_type( type );
  553.    if (s <= 0) {
  554.       gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
  555.       UNDEFARRAY( red );
  556.       UNDEFARRAY( green );
  557.       UNDEFARRAY( blue );
  558.       UNDEFARRAY( alpha );
  559.       UNDEFARRAY( luminance );
  560.       return;
  561.    }
  562.  
  563.    /* process image row by row */
  564.    for (j=0;j<height;j++,y++) {
  565.  
  566.       /*
  567.        * Read the pixels from frame buffer
  568.        */
  569.       if (ctx->Visual->RGBAflag) {
  570.          GLubyte rgba[MAX_WIDTH][4];
  571.      GLfloat rscale = (1.0F / 255.0F);
  572.      GLfloat gscale = (1.0F / 255.0F);
  573.      GLfloat bscale = (1.0F / 255.0F);
  574.      GLfloat ascale = (1.0F / 255.0F);
  575.  
  576.      /* read colors and convert to floats */
  577.          gl_read_rgba_span(ctx, width, x, y, rgba );
  578.      for (i=0;i<width;i++) {
  579.         red[i]   = rgba[i][RCOMP] * rscale;
  580.         green[i] = rgba[i][GCOMP] * gscale;
  581.         blue[i]  = rgba[i][BCOMP] * bscale;
  582.         alpha[i] = rgba[i][ACOMP] * ascale;
  583.      }
  584.  
  585.      if (ctx->Pixel.ScaleOrBiasRGBA) {
  586.         gl_scale_and_bias_color( ctx, width, red, green, blue, alpha );
  587.      }
  588.      if (ctx->Pixel.MapColorFlag) {
  589.         gl_map_color( ctx, width, red, green, blue, alpha );
  590.      }
  591.       }
  592.       else {
  593.      /* convert CI values to RGBA */
  594.      GLuint index[MAX_WIDTH];
  595.      (*ctx->Driver.ReadCI32Span)( ctx, width, x, y, index );
  596.  
  597.      if (ctx->Pixel.IndexShift!=0 || ctx->Pixel.IndexOffset!=0) {
  598.             gl_map_ci( ctx, width, index );
  599.      }
  600.  
  601.          gl_map_ci_to_color(ctx, width, index, red, green, blue, alpha);
  602.       }
  603.  
  604.       if (format==GL_LUMINANCE || format==GL_LUMINANCE_ALPHA) {
  605.          for (i=0;i<width;i++) {
  606.             GLfloat sum = red[i] + green[i] + blue[i];
  607.             luminance[i] = CLAMP( sum, 0.0F, 1.0F );
  608.          }
  609.       }
  610.  
  611.       /*
  612.        * Pack/transfer/store the pixels
  613.        */
  614.  
  615.       /* XXX
  616.        * XXX rewrite this to use the new gl_pack_rgba_span function!!!
  617.        * XXX
  618.        */
  619.  
  620.       switch (type) {
  621.          case GL_UNSIGNED_BYTE:
  622.             {
  623.                GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image(&ctx->Pack,
  624.                               pixels, width, height, format, type, 0, j, 0);
  625.                switch (format) {
  626.                   case GL_RED:
  627.                      for (i=0;i<width;i++)
  628.                         dst[i] = FLOAT_TO_UBYTE(red[i]);
  629.                      break;
  630.                   case GL_GREEN:
  631.                      for (i=0;i<width;i++)
  632.                         dst[i] = FLOAT_TO_UBYTE(green[i]);
  633.                      break;
  634.                   case GL_BLUE:
  635.                      for (i=0;i<width;i++)
  636.                         dst[i] = FLOAT_TO_UBYTE(blue[i]);
  637.                      break;
  638.                   case GL_ALPHA:
  639.                      for (i=0;i<width;i++)
  640.                         dst[i] = FLOAT_TO_UBYTE(alpha[i]);
  641.                      break;
  642.                   case GL_LUMINANCE:
  643.                      for (i=0;i<width;i++)
  644.                         dst[i] = FLOAT_TO_UBYTE(luminance[i]);
  645.                      break;
  646.                   case GL_LUMINANCE_ALPHA:
  647.                      for (i=0;i<width;i++) {
  648.                         dst[i*2+0] = FLOAT_TO_UBYTE(luminance[i]);
  649.                         dst[i*2+1] = FLOAT_TO_UBYTE(alpha[i]);
  650.                      }
  651.                      break;
  652.                   case GL_RGB:
  653.                      for (i=0;i<width;i++) {
  654.                         dst[i*3+0] = FLOAT_TO_UBYTE(red[i]);
  655.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  656.                         dst[i*3+2] = FLOAT_TO_UBYTE(blue[i]);
  657.                      }
  658.                      break;
  659.                   case GL_RGBA:
  660.                      for (i=0;i<width;i++) {
  661.                         dst[i*4+0] = FLOAT_TO_UBYTE(red[i]);
  662.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  663.                         dst[i*4+2] = FLOAT_TO_UBYTE(blue[i]);
  664.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  665.                      }
  666.                      break;
  667.                   case GL_BGR:
  668.                      for (i=0;i<width;i++) {
  669.                         dst[i*3+0] = FLOAT_TO_UBYTE(blue[i]);
  670.                         dst[i*3+1] = FLOAT_TO_UBYTE(green[i]);
  671.                         dst[i*3+2] = FLOAT_TO_UBYTE(red[i]);
  672.                      }
  673.                      break;
  674.                   case GL_BGRA:
  675.                      for (i=0;i<width;i++) {
  676.                         dst[i*4+0] = FLOAT_TO_UBYTE(blue[i]);
  677.                         dst[i*4+1] = FLOAT_TO_UBYTE(green[i]);
  678.                         dst[i*4+2] = FLOAT_TO_UBYTE(red[i]);
  679.                         dst[i*4+3] = FLOAT_TO_UBYTE(alpha[i]);
  680.                      }
  681.                      break;
  682.                   case GL_ABGR_EXT:
  683.                      for (i=0;i<width;i++) {
  684.                         dst[i*4+0] = FLOAT_TO_UBYTE(alpha[i]);
  685.                         dst[i*4+1] = FLOAT_TO_UBYTE(blue[i]);
  686.                         dst[i*4+2] = FLOAT_TO_UBYTE(green[i]);
  687.                         dst[i*4+3] = FLOAT_TO_UBYTE(red[i]);
  688.                      }
  689.                      break;
  690.                   default:
  691.                      gl_problem(ctx, "bad format in glReadPixels\n");
  692.                }
  693.         }
  694.         break;
  695.      case GL_BYTE:
  696.             {
  697.                GLbyte *dst = (GLbyte *) gl_pixel_addr_in_image(&ctx->Pack,
  698.                              pixels, width, height, format, type, 0, j, 0);
  699.                switch (format) {
  700.                   case GL_RED:
  701.                      for (i=0;i<width;i++)
  702.                         dst[i] = FLOAT_TO_BYTE(red[i]);
  703.                      break;
  704.                   case GL_GREEN:
  705.                      for (i=0;i<width;i++)
  706.                         dst[i] = FLOAT_TO_BYTE(green[i]);
  707.                      break;
  708.                   case GL_BLUE:
  709.                      for (i=0;i<width;i++)
  710.                         dst[i] = FLOAT_TO_BYTE(blue[i]);
  711.                      break;
  712.                   case GL_ALPHA:
  713.                      for (i=0;i<width;i++)
  714.                         dst[i] = FLOAT_TO_BYTE(alpha[i]);
  715.                      break;
  716.                   case GL_LUMINANCE:
  717.                      for (i=0;i<width;i++)
  718.                         dst[i] = FLOAT_TO_BYTE(luminance[i]);
  719.                      break;
  720.                   case GL_LUMINANCE_ALPHA:
  721.                      for (i=0;i<width;i++) {
  722.                         dst[i*2+0] = FLOAT_TO_BYTE(luminance[i]);
  723.                         dst[i*2+1] = FLOAT_TO_BYTE(alpha[i]);
  724.                      }
  725.                      break;
  726.                   case GL_RGB:
  727.                      for (i=0;i<width;i++) {
  728.                         dst[i*3+0] = FLOAT_TO_BYTE(red[i]);
  729.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  730.                         dst[i*3+2] = FLOAT_TO_BYTE(blue[i]);
  731.                      }
  732.                      break;
  733.                   case GL_RGBA:
  734.                      for (i=0;i<width;i++) {
  735.                         dst[i*4+0] = FLOAT_TO_BYTE(red[i]);
  736.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  737.                         dst[i*4+2] = FLOAT_TO_BYTE(blue[i]);
  738.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  739.                      }
  740.                      break;
  741.                   case GL_BGR:
  742.                      for (i=0;i<width;i++) {
  743.                         dst[i*3+0] = FLOAT_TO_BYTE(blue[i]);
  744.                         dst[i*3+1] = FLOAT_TO_BYTE(green[i]);
  745.                         dst[i*3+2] = FLOAT_TO_BYTE(red[i]);
  746.                      }
  747.                      break;
  748.                   case GL_BGRA:
  749.                      for (i=0;i<width;i++) {
  750.                         dst[i*4+0] = FLOAT_TO_BYTE(blue[i]);
  751.                         dst[i*4+1] = FLOAT_TO_BYTE(green[i]);
  752.                         dst[i*4+2] = FLOAT_TO_BYTE(red[i]);
  753.                         dst[i*4+3] = FLOAT_TO_BYTE(alpha[i]);
  754.                      }
  755.                   case GL_ABGR_EXT:
  756.                      for (i=0;i<width;i++) {
  757.                         dst[i*4+0] = FLOAT_TO_BYTE(alpha[i]);
  758.                         dst[i*4+1] = FLOAT_TO_BYTE(blue[i]);
  759.                         dst[i*4+2] = FLOAT_TO_BYTE(green[i]);
  760.                         dst[i*4+3] = FLOAT_TO_BYTE(red[i]);
  761.                      }
  762.                      break;
  763.                   default:
  764.                      gl_problem(ctx, "bad format in glReadPixels\n");
  765.                }
  766.             }
  767.         break;
  768.      case GL_UNSIGNED_SHORT:
  769.             {
  770.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  771.                                pixels, width, height, format, type, 0, j, 0);
  772.                switch (format) {
  773.                   case GL_RED:
  774.                      for (i=0;i<width;i++)
  775.                         dst[i] = FLOAT_TO_USHORT(red[i]);
  776.                      break;
  777.                   case GL_GREEN:
  778.                      for (i=0;i<width;i++)
  779.                         dst[i] = FLOAT_TO_USHORT(green[i]);
  780.                      break;
  781.                   case GL_BLUE:
  782.                      for (i=0;i<width;i++)
  783.                         dst[i] = FLOAT_TO_USHORT(blue[i]);
  784.                      break;
  785.                   case GL_ALPHA:
  786.                      for (i=0;i<width;i++)
  787.                         dst[i] = FLOAT_TO_USHORT(alpha[i]);
  788.                      break;
  789.                   case GL_LUMINANCE:
  790.                      for (i=0;i<width;i++)
  791.                         dst[i] = FLOAT_TO_USHORT(luminance[i]);
  792.                      break;
  793.                   case GL_LUMINANCE_ALPHA:
  794.                      for (i=0;i<width;i++) {
  795.                         dst[i*2+0] = FLOAT_TO_USHORT(luminance[i]);
  796.                         dst[i*2+1] = FLOAT_TO_USHORT(alpha[i]);
  797.                      }
  798.                      break;
  799.                   case GL_RGB:
  800.                      for (i=0;i<width;i++) {
  801.                         dst[i*3+0] = FLOAT_TO_USHORT(red[i]);
  802.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  803.                         dst[i*3+2] = FLOAT_TO_USHORT(blue[i]);
  804.                      }
  805.                      break;
  806.                   case GL_RGBA:
  807.                      for (i=0;i<width;i++) {
  808.                         dst[i*4+0] = FLOAT_TO_USHORT(red[i]);
  809.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  810.                         dst[i*4+2] = FLOAT_TO_USHORT(blue[i]);
  811.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  812.                      }
  813.                      break;
  814.                   case GL_BGR:
  815.                      for (i=0;i<width;i++) {
  816.                         dst[i*3+0] = FLOAT_TO_USHORT(blue[i]);
  817.                         dst[i*3+1] = FLOAT_TO_USHORT(green[i]);
  818.                         dst[i*3+2] = FLOAT_TO_USHORT(red[i]);
  819.                      }
  820.                      break;
  821.                   case GL_BGRA:
  822.                      for (i=0;i<width;i++) {
  823.                         dst[i*4+0] = FLOAT_TO_USHORT(blue[i]);
  824.                         dst[i*4+1] = FLOAT_TO_USHORT(green[i]);
  825.                         dst[i*4+2] = FLOAT_TO_USHORT(red[i]);
  826.                         dst[i*4+3] = FLOAT_TO_USHORT(alpha[i]);
  827.                      }
  828.                      break;
  829.                   case GL_ABGR_EXT:
  830.                      for (i=0;i<width;i++) {
  831.                         dst[i*4+0] = FLOAT_TO_USHORT(alpha[i]);
  832.                         dst[i*4+1] = FLOAT_TO_USHORT(blue[i]);
  833.                         dst[i*4+2] = FLOAT_TO_USHORT(green[i]);
  834.                         dst[i*4+3] = FLOAT_TO_USHORT(red[i]);
  835.                      }
  836.                      break;
  837.                   default:
  838.                      gl_problem(ctx, "bad format in glReadPixels\n");
  839.                }
  840.                if (ctx->Pack.SwapBytes) {
  841.                   gl_swap2( (GLushort *) dst, width*n );
  842.                }
  843.             }
  844.         break;
  845.      case GL_SHORT:
  846.             {
  847.                GLshort *dst = (GLshort *) gl_pixel_addr_in_image(&ctx->Pack,
  848.                               pixels, width, height, format, type, 0, j, 0);
  849.                switch (format) {
  850.                   case GL_RED:
  851.                      for (i=0;i<width;i++)
  852.                         dst[i] = FLOAT_TO_SHORT(red[i]);
  853.                      break;
  854.                   case GL_GREEN:
  855.                      for (i=0;i<width;i++)
  856.                         dst[i] = FLOAT_TO_SHORT(green[i]);
  857.                      break;
  858.                   case GL_BLUE:
  859.                      for (i=0;i<width;i++)
  860.                         dst[i] = FLOAT_TO_SHORT(blue[i]);
  861.                      break;
  862.                   case GL_ALPHA:
  863.                      for (i=0;i<width;i++)
  864.                         dst[i] = FLOAT_TO_SHORT(alpha[i]);
  865.                      break;
  866.                   case GL_LUMINANCE:
  867.                      for (i=0;i<width;i++)
  868.                         dst[i] = FLOAT_TO_SHORT(luminance[i]);
  869.                      break;
  870.                   case GL_LUMINANCE_ALPHA:
  871.                      for (i=0;i<width;i++) {
  872.                         dst[i*2+0] = FLOAT_TO_SHORT(luminance[i]);
  873.                         dst[i*2+1] = FLOAT_TO_SHORT(alpha[i]);
  874.                      }
  875.                      break;
  876.                   case GL_RGB:
  877.                      for (i=0;i<width;i++) {
  878.                         dst[i*3+0] = FLOAT_TO_SHORT(red[i]);
  879.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  880.                         dst[i*3+2] = FLOAT_TO_SHORT(blue[i]);
  881.                      }
  882.                      break;
  883.                   case GL_RGBA:
  884.                      for (i=0;i<width;i++) {
  885.                         dst[i*4+0] = FLOAT_TO_SHORT(red[i]);
  886.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  887.                         dst[i*4+2] = FLOAT_TO_SHORT(blue[i]);
  888.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  889.                      }
  890.                      break;
  891.                   case GL_BGR:
  892.                      for (i=0;i<width;i++) {
  893.                         dst[i*3+0] = FLOAT_TO_SHORT(blue[i]);
  894.                         dst[i*3+1] = FLOAT_TO_SHORT(green[i]);
  895.                         dst[i*3+2] = FLOAT_TO_SHORT(red[i]);
  896.                      }
  897.                      break;
  898.                   case GL_BGRA:
  899.                      for (i=0;i<width;i++) {
  900.                         dst[i*4+0] = FLOAT_TO_SHORT(blue[i]);
  901.                         dst[i*4+1] = FLOAT_TO_SHORT(green[i]);
  902.                         dst[i*4+2] = FLOAT_TO_SHORT(red[i]);
  903.                         dst[i*4+3] = FLOAT_TO_SHORT(alpha[i]);
  904.                      }
  905.                   case GL_ABGR_EXT:
  906.                      for (i=0;i<width;i++) {
  907.                         dst[i*4+0] = FLOAT_TO_SHORT(alpha[i]);
  908.                         dst[i*4+1] = FLOAT_TO_SHORT(blue[i]);
  909.                         dst[i*4+2] = FLOAT_TO_SHORT(green[i]);
  910.                         dst[i*4+3] = FLOAT_TO_SHORT(red[i]);
  911.                      }
  912.                      break;
  913.                   default:
  914.                      gl_problem(ctx, "bad format in glReadPixels\n");
  915.                }
  916.                if (ctx->Pack.SwapBytes) {
  917.                   gl_swap2( (GLushort *) dst, width*n );
  918.                }
  919.             }
  920.         break;
  921.      case GL_UNSIGNED_INT:
  922.             {
  923.                GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack,
  924.                              pixels, width, height, format, type, 0, j, 0);
  925.                switch (format) {
  926.                   case GL_RED:
  927.                      for (i=0;i<width;i++)
  928.                         dst[i] = FLOAT_TO_UINT(red[i]);
  929.                      break;
  930.                   case GL_GREEN:
  931.                      for (i=0;i<width;i++)
  932.                         dst[i] = FLOAT_TO_UINT(green[i]);
  933.                      break;
  934.                   case GL_BLUE:
  935.                      for (i=0;i<width;i++)
  936.                         dst[i] = FLOAT_TO_UINT(blue[i]);
  937.                      break;
  938.                   case GL_ALPHA:
  939.                      for (i=0;i<width;i++)
  940.                         dst[i] = FLOAT_TO_UINT(alpha[i]);
  941.                      break;
  942.                   case GL_LUMINANCE:
  943.                      for (i=0;i<width;i++)
  944.                         dst[i] = FLOAT_TO_UINT(luminance[i]);
  945.                      break;
  946.                   case GL_LUMINANCE_ALPHA:
  947.                      for (i=0;i<width;i++) {
  948.                         dst[i*2+0] = FLOAT_TO_UINT(luminance[i]);
  949.                         dst[i*2+1] = FLOAT_TO_UINT(alpha[i]);
  950.                      }
  951.                      break;
  952.                   case GL_RGB:
  953.                      for (i=0;i<width;i++) {
  954.                         dst[i*3+0] = FLOAT_TO_UINT(red[i]);
  955.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  956.                         dst[i*3+2] = FLOAT_TO_UINT(blue[i]);
  957.                      }
  958.                      break;
  959.                   case GL_RGBA:
  960.                      for (i=0;i<width;i++) {
  961.                         dst[i*4+0] = FLOAT_TO_UINT(red[i]);
  962.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  963.                         dst[i*4+2] = FLOAT_TO_UINT(blue[i]);
  964.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  965.                      }
  966.                      break;
  967.                   case GL_BGR:
  968.                      for (i=0;i<width;i++) {
  969.                         dst[i*3+0] = FLOAT_TO_UINT(blue[i]);
  970.                         dst[i*3+1] = FLOAT_TO_UINT(green[i]);
  971.                         dst[i*3+2] = FLOAT_TO_UINT(red[i]);
  972.                      }
  973.                      break;
  974.                   case GL_BGRA:
  975.                      for (i=0;i<width;i++) {
  976.                         dst[i*4+0] = FLOAT_TO_UINT(blue[i]);
  977.                         dst[i*4+1] = FLOAT_TO_UINT(green[i]);
  978.                         dst[i*4+2] = FLOAT_TO_UINT(red[i]);
  979.                         dst[i*4+3] = FLOAT_TO_UINT(alpha[i]);
  980.                      }
  981.                      break;
  982.                   case GL_ABGR_EXT:
  983.                      for (i=0;i<width;i++) {
  984.                         dst[i*4+0] = FLOAT_TO_UINT(alpha[i]);
  985.                         dst[i*4+1] = FLOAT_TO_UINT(blue[i]);
  986.                         dst[i*4+2] = FLOAT_TO_UINT(green[i]);
  987.                         dst[i*4+3] = FLOAT_TO_UINT(red[i]);
  988.                      }
  989.                      break;
  990.                   default:
  991.                      gl_problem(ctx, "bad format in glReadPixels\n");
  992.                }
  993.                if (ctx->Pack.SwapBytes) {
  994.                   gl_swap4( (GLuint *) dst, width*n );
  995.                }
  996.             }
  997.         break;
  998.      case GL_INT:
  999.         {
  1000.                GLint *dst = (GLint *) gl_pixel_addr_in_image(&ctx->Pack,
  1001.                             pixels, width, height, format, type, 0, j, 0);
  1002.                switch (format) {
  1003.                   case GL_RED:
  1004.                      for (i=0;i<width;i++)
  1005.                         dst[i] = FLOAT_TO_INT(red[i]);
  1006.                      break;
  1007.                   case GL_GREEN:
  1008.                      for (i=0;i<width;i++)
  1009.                         dst[i] = FLOAT_TO_INT(green[i]);
  1010.                      break;
  1011.                   case GL_BLUE:
  1012.                      for (i=0;i<width;i++)
  1013.                         dst[i] = FLOAT_TO_INT(blue[i]);
  1014.                      break;
  1015.                   case GL_ALPHA:
  1016.                      for (i=0;i<width;i++)
  1017.                         dst[i] = FLOAT_TO_INT(alpha[i]);
  1018.                      break;
  1019.                   case GL_LUMINANCE:
  1020.                      for (i=0;i<width;i++)
  1021.                         dst[i] = FLOAT_TO_INT(luminance[i]);
  1022.                      break;
  1023.                   case GL_LUMINANCE_ALPHA:
  1024.                      for (i=0;i<width;i++) {
  1025.                         dst[i*2+0] = FLOAT_TO_INT(luminance[i]);
  1026.                         dst[i*2+1] = FLOAT_TO_INT(alpha[i]);
  1027.                      }
  1028.                      break;
  1029.                   case GL_RGB:
  1030.                      for (i=0;i<width;i++) {
  1031.                         dst[i*3+0] = FLOAT_TO_INT(red[i]);
  1032.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  1033.                         dst[i*3+2] = FLOAT_TO_INT(blue[i]);
  1034.                      }
  1035.                      break;
  1036.                   case GL_RGBA:
  1037.                      for (i=0;i<width;i++) {
  1038.                         dst[i*4+0] = FLOAT_TO_INT(red[i]);
  1039.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  1040.                         dst[i*4+2] = FLOAT_TO_INT(blue[i]);
  1041.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  1042.                      }
  1043.                      break;
  1044.                   case GL_BGR:
  1045.                      for (i=0;i<width;i++) {
  1046.                         dst[i*3+0] = FLOAT_TO_INT(blue[i]);
  1047.                         dst[i*3+1] = FLOAT_TO_INT(green[i]);
  1048.                         dst[i*3+2] = FLOAT_TO_INT(red[i]);
  1049.                      }
  1050.                      break;
  1051.                   case GL_BGRA:
  1052.                      for (i=0;i<width;i++) {
  1053.                         dst[i*4+0] = FLOAT_TO_INT(blue[i]);
  1054.                         dst[i*4+1] = FLOAT_TO_INT(green[i]);
  1055.                         dst[i*4+2] = FLOAT_TO_INT(red[i]);
  1056.                         dst[i*4+3] = FLOAT_TO_INT(alpha[i]);
  1057.                      }
  1058.                      break;
  1059.                   case GL_ABGR_EXT:
  1060.                      for (i=0;i<width;i++) {
  1061.                         dst[i*4+0] = FLOAT_TO_INT(alpha[i]);
  1062.                         dst[i*4+1] = FLOAT_TO_INT(blue[i]);
  1063.                         dst[i*4+2] = FLOAT_TO_INT(green[i]);
  1064.                         dst[i*4+3] = FLOAT_TO_INT(red[i]);
  1065.                      }
  1066.                      break;
  1067.                   default:
  1068.                      gl_problem(ctx, "bad format in glReadPixels\n");
  1069.                }
  1070.            if (ctx->Pack.SwapBytes) {
  1071.           gl_swap4( (GLuint *) dst, width*n );
  1072.            }
  1073.         }
  1074.         break;
  1075.      case GL_FLOAT:
  1076.         {
  1077.                GLfloat *dst = (GLfloat *) gl_pixel_addr_in_image(&ctx->Pack,
  1078.                               pixels, width, height, format, type, 0, j, 0);
  1079.                switch (format) {
  1080.                   case GL_RED:
  1081.                      for (i=0;i<width;i++)
  1082.                         dst[i] = red[i];
  1083.                      break;
  1084.                   case GL_GREEN:
  1085.                      for (i=0;i<width;i++)
  1086.                         dst[i] = green[i];
  1087.                      break;
  1088.                   case GL_BLUE:
  1089.                      for (i=0;i<width;i++)
  1090.                         dst[i] = blue[i];
  1091.                      break;
  1092.                   case GL_ALPHA:
  1093.                      for (i=0;i<width;i++)
  1094.                         dst[i] = alpha[i];
  1095.                      break;
  1096.                   case GL_LUMINANCE:
  1097.                      for (i=0;i<width;i++)
  1098.                         dst[i] = luminance[i];
  1099.                      break;
  1100.                   case GL_LUMINANCE_ALPHA:
  1101.                      for (i=0;i<width;i++) {
  1102.                         dst[i*2+0] = luminance[i];
  1103.                         dst[i*2+1] = alpha[i];
  1104.                      }
  1105.                      break;
  1106.                   case GL_RGB:
  1107.                      for (i=0;i<width;i++) {
  1108.                         dst[i*3+0] = red[i];
  1109.                         dst[i*3+1] = green[i];
  1110.                         dst[i*3+2] = blue[i];
  1111.                      }
  1112.                      break;
  1113.                   case GL_RGBA:
  1114.                      for (i=0;i<width;i++) {
  1115.                         dst[i*4+0] = red[i];
  1116.                         dst[i*4+1] = green[i];
  1117.                         dst[i*4+2] = blue[i];
  1118.                         dst[i*4+3] = alpha[i];
  1119.                      }
  1120.                      break;
  1121.                   case GL_BGR:
  1122.                      for (i=0;i<width;i++) {
  1123.                         dst[i*3+0] = blue[i];
  1124.                         dst[i*3+1] = green[i];
  1125.                         dst[i*3+2] = red[i];
  1126.                      }
  1127.                      break;
  1128.                   case GL_BGRA:
  1129.                      for (i=0;i<width;i++) {
  1130.                         dst[i*4+0] = blue[i];
  1131.                         dst[i*4+1] = green[i];
  1132.                         dst[i*4+2] = red[i];
  1133.                         dst[i*4+3] = alpha[i];
  1134.                      }
  1135.                      break;
  1136.                   case GL_ABGR_EXT:
  1137.                      for (i=0;i<width;i++) {
  1138.                         dst[i*4+0] = alpha[i];
  1139.                         dst[i*4+1] = blue[i];
  1140.                         dst[i*4+2] = green[i];
  1141.                         dst[i*4+3] = red[i];
  1142.                      }
  1143.                      break;
  1144.                   default:
  1145.                      gl_problem(ctx, "bad format in glReadPixels\n");
  1146.                }
  1147.            if (ctx->Pack.SwapBytes) {
  1148.           gl_swap4( (GLuint *) dst, width*n );
  1149.            }
  1150.         }
  1151.         break;
  1152.          case GL_UNSIGNED_BYTE_3_3_2:
  1153.             if (format == GL_RGB) {
  1154.                GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image(&ctx->Pack,
  1155.                               pixels, width, height, format, type, 0, j, 0);
  1156.                for (i=0;i<width;i++) {
  1157.                   dst[i] = (((GLint) (red[i]   * 7.0F)) << 5)
  1158.                          | (((GLint) (green[i] * 7.0F)) << 2)
  1159.                          | (((GLint) (blue[i]  * 3.0F))     );
  1160.                }
  1161.             }
  1162.             break;
  1163.          case GL_UNSIGNED_BYTE_2_3_3_REV:
  1164.             if (format == GL_RGB) {
  1165.                GLubyte *dst = (GLubyte *) gl_pixel_addr_in_image(&ctx->Pack,
  1166.                               pixels, width, height, format, type, 0, j, 0);
  1167.                for (i=0;i<width;i++) {
  1168.                   dst[i] = (((GLint) (red[i]   * 7.0F))     )
  1169.                          | (((GLint) (green[i] * 7.0F)) << 3)
  1170.                          | (((GLint) (blue[i]  * 3.0F)) << 5);
  1171.                }
  1172.             }
  1173.             break;
  1174.          case GL_UNSIGNED_SHORT_5_6_5:
  1175.             if (format == GL_RGB) {
  1176.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1177.                                pixels, width, height, format, type, 0, j, 0);
  1178.                for (i=0;i<width;i++) {
  1179.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  1180.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  1181.                          | (((GLint) (blue[i]  * 31.0F))      );
  1182.                }
  1183.             }
  1184.             break;
  1185.          case GL_UNSIGNED_SHORT_5_6_5_REV:
  1186.             if (format == GL_RGB) {
  1187.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1188.                                pixels, width, height, format, type, 0, j, 0);
  1189.                for (i=0;i<width;i++) {
  1190.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  1191.                          | (((GLint) (green[i] * 63.0F)) <<  5)
  1192.                          | (((GLint) (blue[i]  * 31.0F)) << 11);
  1193.                }
  1194.             }
  1195.             break;
  1196.          case GL_UNSIGNED_SHORT_4_4_4_4:
  1197.             if (format == GL_RGB) {
  1198.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1199.                                pixels, width, height, format, type, 0, j, 0);
  1200.                for (i=0;i<width;i++) {
  1201.                   dst[i] = (((GLint) (red[i]   * 15.0F)) << 12)
  1202.                          | (((GLint) (green[i] * 15.0F)) <<  8)
  1203.                          | (((GLint) (blue[i]  * 15.0F)) <<  4)
  1204.                          | (((GLint) (alpha[i] * 15.0F))      );
  1205.                }
  1206.             }
  1207.             break;
  1208.          case GL_UNSIGNED_SHORT_4_4_4_4_REV:
  1209.             if (format == GL_RGB) {
  1210.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1211.                                pixels, width, height, format, type, 0, j, 0);
  1212.                for (i=0;i<width;i++) {
  1213.                   dst[i] = (((GLint) (red[i]   * 15.0F))      )
  1214.                          | (((GLint) (green[i] * 15.0F)) <<  4)
  1215.                          | (((GLint) (blue[i]  * 15.0F)) <<  8)
  1216.                          | (((GLint) (alpha[i] * 15.0F)) << 12);
  1217.                }
  1218.             }
  1219.             break;
  1220.          case GL_UNSIGNED_SHORT_5_5_5_1:
  1221.             if (format == GL_RGB) {
  1222.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1223.                                pixels, width, height, format, type, 0, j, 0);
  1224.                for (i=0;i<width;i++) {
  1225.                   dst[i] = (((GLint) (red[i]   * 31.0F)) << 11)
  1226.                          | (((GLint) (green[i] * 31.0F)) <<  6)
  1227.                          | (((GLint) (blue[i]  * 31.0F)) <<  1)
  1228.                          | (((GLint) (alpha[i] *  1.0F))      );
  1229.                }
  1230.             }
  1231.             break;
  1232.          case GL_UNSIGNED_SHORT_1_5_5_5_REV:
  1233.             if (format == GL_RGB) {
  1234.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1235.                                pixels, width, height, format, type, 0, j, 0);
  1236.                for (i=0;i<width;i++) {
  1237.                   dst[i] = (((GLint) (red[i]   * 31.0F))      )
  1238.                          | (((GLint) (green[i] * 31.0F)) <<  5)
  1239.                          | (((GLint) (blue[i]  * 31.0F)) << 10)
  1240.                          | (((GLint) (alpha[i] *  1.0F)) << 15);
  1241.                }
  1242.             }
  1243.             break;
  1244.          case GL_UNSIGNED_INT_8_8_8_8:
  1245.             if (format == GL_RGBA) {
  1246.                GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack,
  1247.                              pixels, width, height, format, type, 0, j, 0);
  1248.                for (i=0;i<width;i++) {
  1249.                   dst[i] = (((GLint) (red[i]   * 255.0F)) << 24)
  1250.                          | (((GLint) (green[i] * 255.0F)) << 16)
  1251.                          | (((GLint) (blue[i]  * 255.0F)) <<  8)
  1252.                          | (((GLint) (alpha[i] * 255.0F))      );
  1253.                }
  1254.             }
  1255.             else if (format == GL_BGRA) {
  1256.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1257.                                pixels, width, height, format, type, 0, j, 0);
  1258.                for (i=0;i<width;i++) {
  1259.                   dst[i] = (((GLint) (blue[i]  * 255.0F)) << 24)
  1260.                          | (((GLint) (green[i] * 255.0F)) << 16)
  1261.                          | (((GLint) (red[i]   * 255.0F)) <<  8)
  1262.                          | (((GLint) (alpha[i] * 255.0F))      );
  1263.                }
  1264.             }
  1265.             else if (format == GL_ABGR_EXT) {
  1266.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1267.                                pixels, width, height, format, type, 0, j, 0);
  1268.                for (i=0;i<width;i++) {
  1269.                   dst[i] = (((GLint) (alpha[i] * 255.0F)) << 24)
  1270.                          | (((GLint) (blue[i]  * 255.0F)) << 16)
  1271.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  1272.                          | (((GLint) (red[i]   * 255.0F))      );
  1273.                }
  1274.             }
  1275.             break;
  1276.          case GL_UNSIGNED_INT_8_8_8_8_REV:
  1277.             if (format == GL_RGBA) {
  1278.                GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack,
  1279.                              pixels, width, height, format, type, 0, j, 0);
  1280.                for (i=0;i<width;i++) {
  1281.                   dst[i] = (((GLint) (red[i]   * 255.0F))      )
  1282.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  1283.                          | (((GLint) (blue[i]  * 255.0F)) << 16)
  1284.                          | (((GLint) (alpha[i] * 255.0F)) << 24);
  1285.                }
  1286.             }
  1287.             else if (format == GL_BGRA) {
  1288.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1289.                                pixels, width, height, format, type, 0, j, 0);
  1290.                for (i=0;i<width;i++) {
  1291.                   dst[i] = (((GLint) (blue[i]  * 255.0F))      )
  1292.                          | (((GLint) (green[i] * 255.0F)) <<  8)
  1293.                          | (((GLint) (red[i]   * 255.0F)) << 16)
  1294.                          | (((GLint) (alpha[i] * 255.0F)) << 24);
  1295.                }
  1296.             }
  1297.             else if (format == GL_ABGR_EXT) {
  1298.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1299.                                pixels, width, height, format, type, 0, j, 0);
  1300.                for (i=0;i<width;i++) {
  1301.                   dst[i] = (((GLint) (alpha[i] * 255.0F))      )
  1302.                          | (((GLint) (blue[i]  * 255.0F)) <<  8)
  1303.                          | (((GLint) (green[i] * 255.0F)) << 16)
  1304.                          | (((GLint) (red[i]   * 255.0F)) << 24);
  1305.                }
  1306.             }
  1307.             break;
  1308.          case GL_UNSIGNED_INT_10_10_10_2:
  1309.             if (format == GL_RGBA) {
  1310.                GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack,
  1311.                              pixels, width, height, format, type, 0, j, 0);
  1312.                for (i=0;i<width;i++) {
  1313.                   dst[i] = (((GLint) (red[i]   * 1023.0F)) << 22)
  1314.                          | (((GLint) (green[i] * 1023.0F)) << 12)
  1315.                          | (((GLint) (blue[i]  * 1023.0F)) <<  2)
  1316.                          | (((GLint) (alpha[i] *    3.0F))      );
  1317.                }
  1318.             }
  1319.             else if (format == GL_BGRA) {
  1320.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1321.                                pixels, width, height, format, type, 0, j, 0);
  1322.                for (i=0;i<width;i++) {
  1323.                   dst[i] = (((GLint) (blue[i]  * 1023.0F)) << 22)
  1324.                          | (((GLint) (green[i] * 1023.0F)) << 12)
  1325.                          | (((GLint) (red[i]   * 1023.0F)) <<  2)
  1326.                          | (((GLint) (alpha[i] *    3.0F))      );
  1327.                }
  1328.             }
  1329.             else if (format == GL_ABGR_EXT) {
  1330.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1331.                                pixels, width, height, format, type, 0, j, 0);
  1332.                for (i=0;i<width;i++) {
  1333.                   dst[i] = (((GLint) (alpha[i] * 1023.0F)) << 22)
  1334.                          | (((GLint) (blue[i]  * 1023.0F)) << 12)
  1335.                          | (((GLint) (green[i] * 1023.0F)) <<  2)
  1336.                          | (((GLint) (red[i]   *    3.0F))      );
  1337.                }
  1338.             }
  1339.             break;
  1340.          case GL_UNSIGNED_INT_2_10_10_10_REV:
  1341.             if (format == GL_RGBA) {
  1342.                GLuint *dst = (GLuint *) gl_pixel_addr_in_image(&ctx->Pack,
  1343.                              pixels, width, height, format, type, 0, j, 0);
  1344.                for (i=0;i<width;i++) {
  1345.                   dst[i] = (((GLint) (red[i]   * 1023.0F))      )
  1346.                          | (((GLint) (green[i] * 1023.0F)) << 10)
  1347.                          | (((GLint) (blue[i]  * 1023.0F)) << 20)
  1348.                          | (((GLint) (alpha[i] *    3.0F)) << 30);
  1349.                }
  1350.             }
  1351.             else if (format == GL_BGRA) {
  1352.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1353.                                pixels, width, height, format, type, 0, j, 0);
  1354.                for (i=0;i<width;i++) {
  1355.                   dst[i] = (((GLint) (blue[i]  * 1023.0F))      )
  1356.                          | (((GLint) (green[i] * 1023.0F)) << 10)
  1357.                          | (((GLint) (red[i]   * 1023.0F)) << 20)
  1358.                          | (((GLint) (alpha[i] *    3.0F)) << 30);
  1359.                }
  1360.             }
  1361.             else if (format == GL_ABGR_EXT) {
  1362.                GLushort *dst = (GLushort *) gl_pixel_addr_in_image(&ctx->Pack,
  1363.                                pixels, width, height, format, type, 0, j, 0);
  1364.                for (i=0;i<width;i++) {
  1365.                   dst[i] = (((GLint) (alpha[i] * 1023.0F))      )
  1366.                          | (((GLint) (blue[i]  * 1023.0F)) << 10)
  1367.                          | (((GLint) (green[i] * 1023.0F)) << 20)
  1368.                          | (((GLint) (red[i]   *    3.0F)) << 30);
  1369.                }
  1370.             }
  1371.             break;
  1372.          default:
  1373.             gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(type)" );
  1374.       }
  1375.    }
  1376.    UNDEFARRAY( red );
  1377.    UNDEFARRAY( green );
  1378.    UNDEFARRAY( blue );
  1379.    UNDEFARRAY( alpha );
  1380.    UNDEFARRAY( luminance );
  1381. }
  1382.  
  1383.  
  1384.  
  1385. void gl_ReadPixels( GLcontext *ctx,
  1386.                     GLint x, GLint y, GLsizei width, GLsizei height,
  1387.             GLenum format, GLenum type, GLvoid *pixels )
  1388. {
  1389.    if (INSIDE_BEGIN_END(ctx)) {
  1390.       gl_error( ctx, GL_INVALID_OPERATION, "glReadPixels" );
  1391.       return;
  1392.    }
  1393.  
  1394.    if (!pixels) {
  1395.       gl_error( ctx, GL_INVALID_VALUE, "glReadPixels(pixels)" );
  1396.       return;
  1397.    }
  1398.  
  1399.    (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Pixel.ReadBuffer );
  1400.  
  1401.    switch (format) {
  1402.       case GL_COLOR_INDEX:
  1403.          read_index_pixels( ctx, x, y, width, height, type, pixels );
  1404.      break;
  1405.       case GL_STENCIL_INDEX:
  1406.      read_stencil_pixels( ctx, x, y, width, height, type, pixels );
  1407.          break;
  1408.       case GL_DEPTH_COMPONENT:
  1409.      read_depth_pixels( ctx, x, y, width, height, type, pixels );
  1410.      break;
  1411.       case GL_RED:
  1412.       case GL_GREEN:
  1413.       case GL_BLUE:
  1414.       case GL_ALPHA:
  1415.       case GL_RGB:
  1416.       case GL_LUMINANCE:
  1417.       case GL_LUMINANCE_ALPHA:
  1418.       case GL_RGBA:
  1419.       case GL_BGR:
  1420.       case GL_BGRA:
  1421.       case GL_ABGR_EXT:
  1422.          if (!read_fast_rgba_pixels( ctx, x, y, width, height,
  1423.                                      format, type, pixels))
  1424.             read_rgba_pixels( ctx, x, y, width, height, format, type, pixels );
  1425.      break;
  1426.       default:
  1427.      gl_error( ctx, GL_INVALID_ENUM, "glReadPixels(format)" );
  1428.    }
  1429.  
  1430.    (void) (*ctx->Driver.SetBuffer)( ctx, ctx->Color.DrawBuffer );
  1431. }
  1432.